<?php

function download($configs){
	$domain = parse_domain_name($configs['domain']);
	if(!$domain)
		return "无效域名";
	$keywords = format_keywords($configs['keywords'], null, false);
	if(!$keywords)
		return "无效关键词";
	$count = (int)($configs['count']);
	$pages = $configs['pages'];
	$more_pages = trim($configs['more_pages']);
	if($more_pages){
		$more_pages = preg_split('/\s+/', $more_pages);
		for ($i=0; $i<count($more_pages); $i++) {
			if($more_pages[$i][0] != '/' && strpos($more_pages[$i], '://') === false)
				$more_pages[$i] = '/' . $more_pages[$i];
		}
		$pages = array_merge($pages, $more_pages);
	}
	for ($i = 0; $i < count($pages); $i++) {
		$page = $pages[$i];
		if(strpos($page, 'http://') !== 0 && strpos($page, 'https://') !== 0){
			$pages[$i] = "http://{$domain}$page";
		}
	}
	
	$lines = array();
	$format = $_POST['format'];
	if($count <= 0 || $count >= count($keywords) * count($pages)){
		foreach ($pages as $page) {
			foreach ($keywords as $keyword) {
				$lines[] = format($keyword, $page, $format);	//"$keyword,$page,1";
			}
		}
	}else{
		$n = 0;
		if(in_array("http://{$domain}/", $pages)){
			$page = "http://{$domain}/";
			for($i=0; $n<$count && $i<count($keywords); $i++){
				$lines[] = format($keywords[$i], $page, $format);//"{$keywords[$i]},$page,1";
				$n ++;
			}
		}
		
		while($n < $count) {
			$keyword = $keywords[mt_rand(0, count($keywords)-1)];
			$page = $pages[mt_rand(0, count($pages)-1)];
			
			$t = format($keyword, $page, $format);//"$keyword,$page,1";
			if(in_array($t, $lines)){
				continue;
			}
			$lines[] = $t;
			$n++;
		}
	}
	switch ($format){
		case 'link':
		case 'a':
			header('Content-type: text/txt');
			$file = date('Y-m-d ') . $domain . '.txt';
			header('Content-Disposition: attachment; filename="'.$file.'"');
			
			break;
		case 'ubb':
			header('Content-type: text/txt');
			$file = date('Y-m-d ') . $domain . '.txt';
			header('Content-Disposition: attachment; filename="'.$file.'"');
			break;
		case 'chongchong':
		default:
			header('Content-type: text/csv');
			$file = date('Y-m-d ') . $domain . '.csv';
			header('Content-Disposition: attachment; filename="'.$file.'"');
			echo chr(0xEF) . chr(0xBB) . chr(0xBF);
			break;
	}
	foreach ($lines as $line) {
		echo "$line\r\n";
	}
}

function format($keyword, $url, $format) {
	switch ($format){
		case 'link':
		case 'a':
			return "<a href='$url'>{$keyword}</a>";
			break;
		case 'ubb':
			return "[url=$url]{$keyword}[/url]";
			break;
		case 'chongchong':
		default:
			return "{$keyword},{$url},1";
			break;
	}
}

//function 

function check_keywords($keywords) {
	$denys = array('logo', 'price', '2010', 'fake');
	$keywords = preg_split('/[\r\n;,\|]+/', trim($keywords));
	$errors = array();
	$tmp = array_unique($keywords);
	if(count($keywords) != count($tmp)){
		for($i=0; $i<count($keywords); $i++){
			if(!isset($tmp[$i])){
				$errors[] = $keywords[$i] . ' 重复';
				continue;
			}
		}
	}
	$keywords = $tmp;
	for($i=0; $i<count($keywords); $i++){
		if(isset($keywords[$i]) && strlen($keywords[$i]) < 3){
			$errors[] = '短关键字: ' . $tmp[$i];
			continue;
		}
	}
	$tmp = array();
	foreach ($keywords as $keyword) {
		$words = preg_split('/[\s　]+/', $keyword);
		foreach ($words as $word) {
			if(in_array($word, $denys))
				$errors[] = '出现单词: ' . $word;
		}
		sort($words);
		$tmp[] = implode(';', $words);
	}
	$tmp2 = array_unique($tmp);
	if(count($tmp) != count($tmp2)){
		for($i=0; $i<count($tmp); $i++){
			if(!isset($tmp2[$i])){
				$errors[] = $keywords[$i] . ' 仅词序不一样';
			}
		}
	}
	$ks = array();
	if($errors) {
		$errors = array_unique($errors);
		foreach ($errors as $error) {
			echo "$error<br/>";
		}
	}
}

function format_keywords($keywords, $format, $echo=true){
	$spinner = $format === 'spinner';
	$keywords = trim($keywords);
	if(preg_match('/^\{.+\|.+\}$/', $keywords)){
		$keywords = explode('|', substr($keywords, 1, -1));
	}elseif(preg_match('/^[^\r\n;,]+$/', $keywords)){
		$keywords = preg_split('/[ \t\|]+/', $keywords);
	}else{
		$keywords = preg_split('/[\r\n;,\|]+/', $keywords);
	}
	for ($i=0; $i<count($keywords); $i++) {
		$keywords[$i] = trim($keywords[$i]);
	}
	$keywords = array_unique($keywords);
	if(count($keywords) == 1){
		if($echo)
			echo $keywords[0];
		return $keywords[0];
	}
	if(!$echo)
		return $keywords;
	$t = false;
	if($spinner){
		echo '{';
	}
	foreach ($keywords as $keyword) {
		if(!$t){
			$t = true;
		}else{
			echo $spinner ? '|' : ($format ? $format : "\n");
		}
		echo $keyword;
	}
	if($spinner){
		echo '}';
	}
	return $result;
}

function unique_keywords($keywords, $deep) {
	if(!$keywords)
		return;
	$keywords = format_keywords($keywords, "\n", false);
	$keywords = array_unique($keywords);
	if($deep) {
		$tmp = array();
		foreach ($keywords as $keyword) {
			$t = preg_split('/\s+/', $keyword);
			sort($t);
			$t = implode(' ', $t);
			$tmp[] = $t;
		}
		$tmp2 = array_unique($tmp);
		for($i=0; $i<count($tmp); $i++){
			if(!isset($tmp2[$i])){
				unset($keywords[$i]);
			}
		}
	}
	$t = false;
	foreach ($keywords as $keyword) {
		if($t)
			echo "\n";
		echo $keyword;
		$t = true;
	}
}
